<?php
class FriendsController extends SilkControllerBase {
	
	public function index($params) {
		if(!isset($params["level"])) {
			$params["level"] = 1;
		}
		$next_level = $params["level"] + 1;

		$this->breadCrumbs($params);
		switch( $params["level"] ) {
			case 10:
				break;
			case 9:
				break;
			case 8:
				break;
			case 7: 
				break;
			case 6:
				break;
			case 5:
				break;
			case 4:
				break;
			case 3:
				if($params["p3"] == "M" || $params["p3"] == "F") {
					$results = orm("mpUser")->find_all(array(	"conditions" => array("gender = '" . $params["p3"] . "'"),
																"joins" => "INNER JOIN silk_portfolios on silk_users.id = silk_portfolios.user_id AND silk_portfolios.type='modelportfolio'"));
					break;
				}
				if(is_numeric($params["p3"])) {
					$results = orm("mpUser")->find_all(array("joins" => "INNER JOIN silk_portfolios on silk_users.id = silk_portfolios.user_id AND silk_portfolios.experience_id=".$params["p3"]." AND silk_portfolios.type='".$params["p2"]."Portfolio'"));
				}
				break;
			case 2:
				switch( $params["p2"]) {
					case "Model":
						$results = array("F" => "Female", "M" => "Male");
						break;
					case "Photographer":
						$results = orm("Experience")->find_all(array(	"conditions" => array("active = 1"),
																		"order" => "name ASC"));
						break;
				}
				break;
			case 1:
				$results = orm("UserType")->find_all(array(	"conditions" => array("active = 1"),
															"order" => "name ASC"));
				break;
			default:
				break;
		}
		
		$baseLink = SilkResponse::create_url(array(	"controller" => "friends",
															"action" => "index",
															"level" => $next_level));
		$baseLink .= $this->add_params_to_baseLink($params);
		if(isset($params["select"])) $this->set("select", $params["select"]);
		if(isset($baseLink)) $this->set("baseLink", $baseLink);
		if(isset($params["level"])) $this->set("level", $params["level"]);
		if(isset($results)) $this->set("results", $results);
	}
	
	public function add_params_to_baseLink($params, $breadcrumbs = 10) {
		$parameter = 0;
		$extra_params = "";
		
		while($parameter <= 10) {
			if(isset($params["p".$parameter])) {
				if($breadcrumbs >= $parameter) {
					$extra_params .= "&p" . $parameter . "=" . $params["p".$parameter];
					$this->set("p".$parameter, $params["p".$parameter]);
				}
			}
			$parameter++;
		}
		return $extra_params;
	}
	
	public function breadCrumbs($params) {
		$links = array();
		
		switch( $params["level"] ) {
			case 10:
			case 9:
			case 8:
			case 7:
			case 6:
			case 5:
			case 4:
			case 3:
				if(!is_numeric($params["p3"])) {
					$links[3]["Gender"] = SilkResponse::create_url(array(	"controller" => "friends",
																			"action" => "index",
																			"level" => 2))
																			. $this->add_params_to_baseLink($params, 2);
				} else {
					$links[3]["Experience"] = SilkResponse::create_url(array(	"controller" => "friends",
																				"action" => "index",
																				"level" => 2))
																				. $this->add_params_to_baseLink($params, 2);
				}
			case 2:
				$links[2]["User Types"] = SilkResponse::create_url(array(	"controller" => "friends",
																			"action" => "index",
																			"level" => 1))
																			. $this->add_params_to_baseLink($params, 1);
			case 1:
				$links[1]["Home"] = SilkResponse::create_url(array(	"controller" => "friends",
																	"action" => "index",
																	));
				break;
		}
		$this->set("links", array_reverse($links));
//		SilkGreg::barf("links", ($links));
	}
}

?>